//
//  sanHomeTopView.m
//  sanFsports
//
//  Created by mac on 16/11/20.
//  Copyright © 2016年 sanAiFuSport.com. All rights reserved.
//

#import "sanHomeTopView.h"
#import "CZAdditions.h"
#import "Masonry.h"


#define btnW [UIScreen mainScreen].bounds.size.width / 4

@interface sanHomeTopView ()

/**
 第一个按钮
 */
@property (weak, nonatomic) UIButton *firstBtn;

/**
 *  xiantiao
 */
@property (weak, nonatomic) UIView *lineView;

/**
 *  负责记录选中的按钮
 */
@property (weak, nonatomic) UIButton *selectBtn;

@end

@implementation sanHomeTopView
{
    //保存3个按钮的集合
    NSArray<UIButton *> *_btnsArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark-- 处理按钮的点击时间
- (void)categoryBtnClick:(UIButton *)sender
{
    _pageNumber = [_btnsArr indexOfObject:sender];
        
    //3 修改按钮的状态
    _selectBtn.selected = NO;
    sender.selected = YES;
    _selectBtn = sender;
    
}

#pragma mark-- 搭建界面
- (void)setupUI
{
    NSMutableArray <UIButton*>*btns = [NSMutableArray array];
    
    NSArray *btnTitles = @[@"选择日期", @"区域筛选", @"显示位置", @"推荐排序"];
    
    for (int i = 0; i < 4; i++) {
        
        UIButton *btn = [UIButton new];
        
        [btns addObject:btn];
        
//        [btn setBackgroundColor:[UIColor magentaColor]];
        
        [btn setTag:i + 1];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.left.offset(i * btnW);
            make.width.offset(btnW);
        }];
    }
    
    [btnTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [btns[idx] setTitle:obj forState:UIControlStateNormal];
        [btns[idx] setTitle:obj forState:UIControlStateHighlighted];
        
    }];
}

- (void)btnClick:(UIButton *)btn {
    
    switch (btn.tag) {
        case 1:
            
            self.modalBlock(btn.tag);
            break;
            
        case 2:
            
            self.modalBlock(btn.tag);
            break;
            
        case 3:
            
            self.modalBlock(btn.tag);
            break;
            
        case 4:
            
            self.modalBlock(btn.tag);
            break;

            
        default:
            break;
    }
}

@end
